package sf.database.template.expression;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sf.database.OrmConfig;
import sf.database.jdbc.sql.SQLContext;
import sf.database.template.TemplateHandler;
import sf.database.template.TemplateRender;
import sf.database.template.TemplateType;
import sf.database.template.sql.SqlHelp;
import sf.tools.IOUtils;
import sf.tools.StringUtils;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ExpressionHandler implements TemplateHandler {
    private static final Logger logger = LoggerFactory.getLogger(ExpressionHandler.class);
    private static ExpressionHandler instance;

    public static ExpressionHandler getInstance() {
        if (instance == null) {
            instance = new ExpressionHandler();
        }
        return instance;
    }

    private static volatile boolean initialized = false;

    private ExpressionHandler() {

    }

    @Override
    public void loadAllSQL() {
        Set<URL> set = Collections.emptySet();
        Map<String, String> pathMap = OrmConfig.getInstance().getSqlTemplatePaths();
        if (pathMap != null && pathMap.containsKey(TemplateType.expression.name())) {
            String paths = pathMap.get(TemplateType.expression.name());
            if (StringUtils.isNotBlank(paths)) {
                set = TemplateRender.getSqlTemplates(StringUtils.split(paths, ","));
            }
        } else {
            set = TemplateRender.getDefaultSqlTemplates();
        }
        Map<String, String> map = new HashMap<>();
        for (URL r : set) {
            try {
                List<String> lines = new ArrayList<>();
                IOUtils.asStringLambda(r.openStream(), StandardCharsets.UTF_8, true, line -> {
                    lines.add(line);
                    return true;
                });
                //必须有内容
                map.putAll(SqlHelp.fromMarkdown(lines));
            } catch (IOException e) {
                logger.error("", e);
            }
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {

        }
        initialized = true;
    }

    @Override
    public String getSQL(String sqlId) {
        if (!initialized) {
            loadAllSQL();
            //是否开启debug模式
            if (!OrmConfig.getInstance().isSqlTemplateDebug()) {// 非debug模式则初始化一次
                initialized = true;
            }
        }
        return null;
    }

    @Override
    public SQLContext getParsedSql(String sqlId, String dbType, Map<String, Object> parameters) {
        return null;
    }

    @Override
    public SQLContext getParsedSqlSource(String sqlSource, Map<String, Object> parameters) {
        return null;
    }

    @Override
    public SQLContext getParsedPageSql(String sqlId, String dbType, Map<String, Object> parameters) {
        return null;
    }

    @Override
    public SQLContext getParsedPageSqlSource(String sqlSource, Map<String, Object> parameters) {
        return null;
    }

    @Override
    public SQLContext getParsedPageCountSql(String sqlId, String dbType, Map<String, Object> parameters) {
        return null;
    }

    @Override
    public SQLContext getParsedPageCountSqlSource(String sqlSource, Map<String, Object> parameters) {
        return null;
    }

    @Override
    public SQLContext getParsedPageListSql(String sqlId, String dbType, Map<String, Object> parameters) {
        return null;
    }

    @Override
    public SQLContext getParsedPageListSqlSource(String sqlSource, Map<String, Object> parameters) {
        return null;
    }
}
